package com.springboot.frame.oauth.server.security.hadler;

import com.springboot.frame.oauth.server.utils.ResponseUtils;
import com.springboot.frame.oauth.server.utils.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * security 登录失败的实现
 */
@Component
@Slf4j
public class SecurityAuthenticationFailureHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) {
        log.info("登录失败: " + exception.getMessage());
        // JSON 格式的返回
        ResultCode.LOGIN_ERROR.message = exception.getMessage();
        // JSON 格式的返回
        ResponseUtils.renderJson(response, ResultCode.LOGIN_ERROR);
    }

}
